package itbour.onetouchshow.widget;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import itbour.onetouchshow.R;
import itbour.onetouchshow.utils.UIUtils;

/**
 * Created by Yang on 2017/12/16.
 */

public class BootDialog extends DialogFragment {
    View rootView;
    @BindView(R.id.tv_try_again)
    TextView tvTryAgain;
    @BindView(R.id.id_tv_make_over)
    TextView idTvMakeOver;
    Unbinder unbinder;
    @BindView(R.id.id_ig_cancle)
    ImageView idIgCancle;
    BootOnListener onListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = UIUtils.inflate(R.layout.dialog_boot);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //此处可以设置Dialog的style等等
        super.onCreate(savedInstanceState);
        //无法直接点击外部取消dialog
        setCancelable(false);
        //NO_FRAME就是dialog无边框，0指的是默认系统Theme
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.dialog_transparent_style);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams windowParams = window.getAttributes();
        //windowParams.dimAmount = 0.0f;
        // windowParams.y = 100;
        window.setAttributes(windowParams);
        Dialog dialog = getDialog();
        if (dialog != null) {
            int width = UIUtils.WHD()[0] / 4 * 3;
            int height = (int) (UIUtils.WHD()[0] / 4 * 3 * 1.328);
            dialog.getWindow().setLayout(width, height);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void builder(Activity activity) {
        show(activity.getFragmentManager().beginTransaction(), "dialogUtils");
    }

    @OnClick({R.id.tv_try_again, R.id.id_tv_make_over, R.id.id_ig_cancle})
    public void onViewClicked(View view) {
        dismiss();
        switch (view.getId()) {
            case R.id.tv_try_again:
                if (onListener != null) {
                    onListener.onTryagain();
                }
                break;
            case R.id.id_tv_make_over:
                if (onListener != null) {
                    onListener.onMakeOver();
                }
                break;
            case R.id.id_ig_cancle:
                if (onListener != null) {
                    onListener.onMakeOver();
                }
                break;
        }
    }

    public BootOnListener getOnListener() {
        return onListener;
    }

    public void setOnListener(BootOnListener onListener) {
        this.onListener = onListener;
    }

    public interface BootOnListener {
        void onTryagain();

        void onMakeOver();
    }
}
